package com.zizo.autohome.kb.crawl;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Document.OutputSettings;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;

import com.zizo.autohome.kb.bean.AutoHomeKB;
import com.zizo.autohome.kb.dao.KBDaoOfAutoHome;
import com.zizo.core.Crawl;
import com.zizo.core.DAO;
import com.zizo.util.Analysis;
import com.zizo.util.DocUtil;
import com.zizo.util.Util;

public class KBCrawlOfAutoHome extends Crawl {
	private Map<String, String> syn;

	public KBCrawlOfAutoHome(String id, DAO dao, Map<String, String> syn) {
		super(id, dao);
		this.syn = syn;
	}

	/**
	 * @Title: exchangValue
	 * @Description: 用户购买目的进行String To Integer的转换
	 * @param s
	 * @return
	 * @return: int
	 * @author: Alexis
	 */
	public int exchangValue(String s) {
		int res = 0;
		switch (s) {
		case "上下班":
			res = 1;
			break;
		case "接送小孩":
			res = 2;
			break;

		case "购物":
			res = 3;
			break;

		case "自驾游":
			res = 4;
			break;

		case "商务接送":
			res = 5;
			break;

		case "跑长途":
			res = 6;
			break;

		case "拉货":
			res = 7;
			break;

		case "越野":
			res = 8;
			break;

		case "赛车":
			res = 9;
			break;

		case "泡妞":
			res = 10;
			break;

		default:
			break;
		}
		return res;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getPages
	 * 
	 * @Description:
	 * 
	 * @param doc
	 * 
	 * @param selectStr
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.CrawlInterface#getPages(org.jsoup.nodes.Document,
	 * java.lang.String)
	 */
	@Override
	public int getPages(Document doc, String selectStr) {
		String page = doc.select(selectStr).get(0).text();
		return Integer.parseInt(page.substring(1, page.length() - 1));
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getItems
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#getItems(java.lang.String)
	 */
	@Override
	public void getItems(String url) {
		KBDaoOfAutoHome kbdao = (KBDaoOfAutoHome) this.dao;
		// url="http://k.autohome.com.cn/" + this.id + "/"
		Document docPage = DocUtil.getDoc(url);
		if (docPage == null || docPage.text().length() < 200) {
			return;
		}
		int pages = 1;
		/* 如果口碑的帖子数大于15，则显示的页数大于1页 */
		if (docPage.select("div.page").size() > 0) {
			pages = getPages(docPage, "div.page-cont div.page span.page-item-info");
		}
		int count = 0;
		for (int i = 1; i < pages + 1; i++) {
			String resUrl = "";
			if (url.contains("stopselling")) {
				resUrl = "http://k.autohome.com.cn/" + this.id + "/StopSelling/index_" + i + ".html";
			} else {
				resUrl = "http://k.autohome.com.cn/" + this.id + "/index_" + i + ".html";
			}
			Document doc = null;
			if (i == 1) {
				doc = docPage;
			} else {
				doc = DocUtil.getDoc(resUrl);
			}
			if (doc.text().contains("您访问的页面出错了！") || doc == null) {
				break;
			}
			Elements items = doc.select("div.mouthcon div.mouthcon-cont");
			for (Element item : items) {
				try {
					Element titleName = item.select("div.mouth-main div.cont-title div.title-name a").get(0);
					String kburl = titleName.attr("href").split("_")[1].trim();
					int kb_kbid = Integer.parseInt(kburl);// 口碑id
					Element userName = item.select("div.mouthcon-cont-left div.name-text a").get(0);
					String kb_yhmc = userName.text();// 用户名称
					String yhid = userName.attr("href").split("cn/")[1].trim();
					int kb_yhid = Integer.parseInt(yhid);// 用户id
					float ite[] = new float[8];
					Elements star = item.select("div.position-r span.c333");// 左侧星级评价
					for (int k = 0; k < star.size(); k++) {
						Element var = star.get(k);
						ite[k] = Float.parseFloat(var.text());
					}
					Elements chooses = item.select("div.mouthcon-cont-left").select("div.choose-con")
							.select("div.mt-10>dl.choose-dl:not(.border-b-no)");// 购车目的
					Map<String, String> box = new HashMap<String, String>();
					for (Element c : chooses) {
						String key = c.select("dt").text();
						String value = "";
						switch (key) {
						case "购买车型":
							String carBrandID = c.select("dd a").get(0).attr("href").split("/")[1];
							String carModelID = c.select("dd a").get(1).attr("href").split("/")[2];
							value = carBrandID + "-" + carModelID;
							break;
						case "购买地点":
							String shopaddr = c.select("dd.c333").text().trim();
							value = shopaddr;
							break;
						case "购车经销商":
							value = c.select("a").attr("data-val").split(",")[1];
							break;
						case "购买时间":
							String sj = c.select("dd.bg-blue").get(0).text();
							String year = sj.split("年")[0];
							String month = sj.substring(5, sj.indexOf("月"));
							String shopTime = year + "-" + month + "-" + "01";
							value = shopTime;
							break;
						case "裸车购买价":
							String tag = c.select("dd.font-arial").select("dd.bg-blue").text();
							String lcj = tag.split("万")[0];
							value = lcj.substring(0, lcj.length() - 1);
							break;
						case "油耗 目前行驶":
							Element e1 = c.select("dd.bg-blue p").get(0);
							Element e2 = c.select("dd.bg-blue p").get(1);
							String syyh = e1.text().split("升/百公里")[0].substring(0, e1.text().indexOf("升") - 1);
							String lc = e2.text().split("公里")[0].substring(0, e2.text().indexOf("公") - 1);
							value = syyh + "-" + lc;
							break;
						default:
							break;
						}
						box.put(key, value);
					}
					int kb_ppid = Integer.parseInt(box.get("购买车型").split("-")[0]);// 品牌id
					int kb_cxid = Integer.parseInt(box.get("购买车型").split("-")[1]);// 系列id
					String kb_gmdd = box.get("购买地点");// 购买地点
					String kb_gmsj = box.get("购买时间");// 购买时间
					String kb_gmqy = box.get("购车经销商");// 购买区域
					String kb_jxs = box.get("购车经销商");// 经销商
					float kb_gmjg = Float.parseFloat(box.get("裸车购买价"));// 购买价格
					float kb_syyh = 0;
					int kb_lc = 0;
					try {
						kb_syyh = Float.parseFloat(box.get("油耗 目前行驶").split("-")[0]);
						kb_lc = Integer.parseInt(box.get("油耗 目前行驶").split("-")[1]);
					} catch (Exception e1) {
					}
					float kb_kj = ite[0];// 空间
					float kb_dl = ite[1];// 动力
					float kb_ck = ite[2];// 操控
					float kb_yh = ite[3];// 油耗
					float kb_ssx = ite[4];// 舒适性
					float kb_wg = ite[5];// 外观
					float kb_ns = ite[6];// 内室
					float kb_xjb = ite[7];// 性价比
					String zcs = item.select("label.supportNumber").get(0).text();
					int kb_zcs = Integer.parseInt(zcs);// 支持数
					String lls = item.select("a.orange").text();
					int kb_lls = Integer.parseInt(lls);// 浏览数
					Element tag12 = item.select("span.font-arial").select("span.CommentNumber").get(0);
					String pls = tag12.text();
					int kb_pls = Integer.parseInt(pls);// 评论数
					java.util.Date zqsj = new java.util.Date();
					java.sql.Date kb_zqsj = new Date(zqsj.getTime());// 抓取时间
					String kb_gcmd = item.select("div.choose-con dl.choose-dl dd.fn-clear").get(0).text();
					String[] gcmd = kb_gcmd.split(" ");
					for (int j = 0; j < gcmd.length; j++) {
						int kb_gcyt = exchangValue(gcmd[j]);
						kbdao.insertGCYT(kb_ppid, kb_kbid, kb_gcyt);
					}
					Element content = item.select("div.cont-title div.title-name a").get(0);
					String link = content.attr("href");
					Document cont = DocUtil.getDoc(link);
					if (cont == null || cont.text().contains("您访问的页面出错了！")) {
						break;
					}
					Elements lis = cont.select("div.mouth-item");
					String kb_plrq = "2000-01-01";
					for (Element ee : lis) {
						String iEle = ee.select("i.icon").get(0).text();
						String plrq = ee.select("div.name-width-01 b").get(0).text();
						kb_plrq = plrq.substring(1);// 评论日期
						String kb_plnr = "";
						int kb_plcd = 0;
						if (iEle.equals("追加")) {
							String nr = ee.select("div.text-con dd.add-dl-text").html();
							kb_plnr = filterData(nr);
							kb_plcd = kb_plnr.length();// 评论长度
						} else if (iEle.equals("口碑")) {
							String nr = ee.select("div.text-con").html();
							kb_plnr = filterData(nr);
							kb_plcd = kb_plnr.length();// 评论长度
						}
						try {
							kb_gmdd = Util.getMatched(kb_gmdd, "(\\S{1,})(\\s+)").group(1);
						} catch (Exception e) {
						}
						kbdao.insertNUM(kb_kbid, kb_ppid, kb_cxid, kb_plrq, kb_lls, kb_pls);// 插入浏览数回复数
						AutoHomeKB kb = new AutoHomeKB(kb_kbid, kb_ppid, kb_cxid, kb_yhmc, kb_yhid, kb_plrq, kb_gmdd,
								kb_gmqy, kb_jxs, kb_kj, kb_dl, kb_ck, kb_yh, kb_ssx, kb_wg, kb_ns, kb_xjb,
								Util.removeFullWidth(kb_plnr.trim()), kb_plcd, kb_gmsj, kb_gmjg, kb_syyh, kb_lc, kb_zcs,
								kb_lls, kb_pls, kb_zqsj, kb_gcmd);
						kbdao.insertData(kb);
						kbdao.insertSynData(new AutoHomeKB(kb_kbid, kb_ppid, kb_cxid, kb_yhmc, kb_yhid, kb_plrq,
								kb_gmdd, kb_gmqy, kb_jxs, kb_kj, kb_dl, kb_ck, kb_yh, kb_ssx, kb_wg, kb_ns, kb_xjb,
								Analysis.replaceSyn(kb_plnr.trim(), syn), kb_plcd, kb_gmsj, kb_gmjg, kb_syyh, kb_lc,
								kb_zcs, kb_lls, kb_pls, kb_zqsj, kb_gcmd));
						count++;
					}
				} catch (Exception a) {
					a.printStackTrace();
				}
			}
		}
		System.out.println(this.id + "结束" + count);
	}

	/**
	 * @Title: filterData
	 * @Description: 格式化数据，主要应用在评论内容的换行显示
	 * @param content
	 * @return
	 * @return: String
	 * @author: Alexis
	 */
	public String filterData(String content) {
		OutputSettings settings = new OutputSettings();
		settings.prettyPrint(false);
		String res = Parser.unescapeEntities(Jsoup.clean(content, "", Whitelist.none(), settings),false).trim();// 评论内容
		return res;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: crawlContent
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#crawlContent(java.lang.String)
	 */
	@Override
	public void crawlContent(String url) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: run
	 * 
	 * @Description:
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		getItems("http://k.autohome.com.cn/" + this.id + "/");// 在售
		getItems("http://k.autohome.com.cn/" + this.id + "/stopselling/");// 停售
	}

//	public static void main(String[] args) throws Exception {
//		Connection currConn = ConnectionPool.getConnection("autohome_other", "localhost", "3306");
//		KBDaoOfAutoHome kbdao = new KBDaoOfAutoHome(currConn, null);
////		ExecutorService fixexecutorservice = Executors.newFixedThreadPool(15);
////		List<String> carIDs = kbdao.getIDs();
//		Map<String, String> syn = kbdao.getSyn();
////		List<String> updates = new ArrayList<String>();
////		Statement st = null;
////		ResultSet rs = null;
////		String sql = "SELECT updated FROM autohome_other.update";
////		try {
////			st = currConn.createStatement();
////			rs = st.executeQuery(sql);
////			while (rs.next()) {
////				updates.add(rs.getString("updated"));
////			}
////		} catch (SQLException e) {
////			e.printStackTrace();
////		}
////	   carIDs.removeAll(updates);
////		for (String carID : carIDs) {
////		fixexecutorservice.execute(new KBCrawlOfAutoHome(carID, kbdao, syn));
////		}
////		fixexecutorservice.shutdown();
//		KBCrawlOfAutoHome kb=new KBCrawlOfAutoHome("4158", kbdao, syn);
//		kb.run();
//   }
}
